//
//  JCClubMemberCell.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/22.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCClubMemberCell.h"
#import "JCClubMemberModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface JCClubMemberCell()

@property (weak, nonatomic) IBOutlet UILabel *rankLabel;
@property (weak, nonatomic) IBOutlet UIImageView *userHeaderImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userDetailLabel;
@property (weak, nonatomic) IBOutlet UIView *myView;

@property (nonatomic, strong) JCClubMemberModel *model;
@property (nonatomic, assign) ClubMemberRankType rankType;

@end

@implementation JCClubMemberCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _userHeaderImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    _userHeaderImageView.layer.borderWidth = 2;
    _userHeaderImageView.layer.cornerRadius = 20;
}

- (void)rankType:(ClubMemberRankType)rankType
       rankModel:(JCClubMemberModel *)rankModel
       indexPath:(NSIndexPath *)indexPath{
    _model = rankModel;
    _rankType = rankType;
    
    [_userHeaderImageView sd_setImageWithURL:rankModel.icon placeholderImage:kHeaderPlaceholdImage];
    _userNameLabel.text = rankModel.userName;
    _rankLabel.text = @(indexPath.row + 1).stringValue;
    if (rankType == ClubMemberRankTypeDuration) {
        if (rankModel.duration < 60) {
            _userDetailLabel.text = [NSString stringWithFormat:@"%ld'",rankModel.duration%60];
        }else{
            _userDetailLabel.text = [NSString stringWithFormat:@"%ld:%ld'",rankModel.duration/60,rankModel.duration%60];
        }
    }else if (rankType == ClubMemberRankTypeSwings){
        _userDetailLabel.text = [NSString stringWithFormat:@"%ld",rankModel.swings];
    }else if (rankType == ClubMemberRankTypeMaxSpeed){
        if(![YFUserDefaults generalMetricSystemUnit]){
            _userDetailLabel.text = [[NSString stringWithFormat:@"%.1f",[Common kmhToMph:round(rankModel.maxSpeed)]] stringByAppendingString:@"mph"];
        }else{
            _userDetailLabel.text = [NSString stringWithFormat:@"%ld kph",rankModel.maxSpeed];
        }
    }
    
    if ([rankModel.userID isEqualToString:[User getLastUser].userID]) {
        _myView.hidden = NO;
    }else{
        _myView.hidden = YES;
    }
}

- (void)memberModel:(JCClubMemberModel *)memberModel indexPath:(NSIndexPath *)indexPath{
    _model = memberModel;
    _rankLabel.hidden = NO;
    _myView.hidden = YES;
    [_userHeaderImageView sd_setImageWithURL:memberModel.icon placeholderImage:kHeaderPlaceholdImage];
    _userNameLabel.text = memberModel.userName;
    _rankLabel.text = @(indexPath.row + 1).stringValue;
}

- (JCClubMemberModel *)getMemberModel{
    return _model;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
